What is orderedmap?
The orderedmap npm package provides a data structure that maintains the order of keys as they are added. This is useful for scenarios where the order of elements is important, such as in configuration files, ordered collections, or when you need predictable iteration order.
What are orderedmap's main functionalities?
Creating an OrderedMap
This feature allows you to create a new instance of an OrderedMap. The OrderedMap will maintain the order of keys as they are added.
const OrderedMap = require('orderedmap');
const map = new OrderedMap();
Adding and Retrieving Elements
You can add elements to the OrderedMap using the set method and retrieve them using the get method. The order of keys is preserved.
map.set('key1', 'value1');
map.set('key2', 'value2');
console.log(map.get('key1')); // Outputs: 'value1'
Iterating Over Elements
This feature allows you to iterate over the elements in the OrderedMap in the order they were added. This is useful for scenarios where the order of elements is important.
map.set('key1', 'value1');
map.set('key2', 'value2');
for (let [key, value] of map) {
console.log(key, value);
}
Deleting Elements
You can delete elements from the OrderedMap using the delete method. This will remove the element and maintain the order of the remaining elements.
map.set('key1', 'value1');
map.delete('key1');
console.log(map.has('key1')); // Outputs: false
Other packages similar to orderedmap
immutable
The immutable package provides a variety of immutable data structures, including OrderedMap. It offers more features and guarantees immutability, which can be beneficial for functional programming and state management in applications like React.
es6-map
The es6-map package is a polyfill for the ES6 Map, which maintains the order of keys as they are added. While it is not specifically designed for ordered maps, it provides similar functionality and is a standard part of modern JavaScript.
OrderedMap
Persistent data structure representing an ordered mapping from strings
to values, with some convenient update methods.
This is not an efficient data structure for large maps, just a minimal
helper for cleanly creating and managing small maps in a way that
makes their key order explicit and easy to think about.
License: MIT
Reference
The exported value from this module is the class OrderedMap
,
instances of which represent a mapping from strings to arbitrary
values.
OrderedMap.from
(value: ?Object | OrderedMap) → OrderedMap
Return a map with the given content. If null, create an empty map. If
given an ordered map, return that map itself. If given an object,
create a map from the object's properties.
Methods
Instances of OrderedMap
have the following methods and properties:
get
(key: string) → ?any
Retrieve the value stored under key
, or return undefined when
no such key exists.
update
(key: string, value: any, newKey: ?string) → OrderedMap
Create a new map by replacing the value of key
with a new
value, or adding a binding to the end of the map. If newKey
is
given, the key of the binding will be replaced with that key.
remove
(key: string) → OrderedMap
Return a map with the given key removed, if it existed.
addToStart
(key: string, value: any) → OrderedMap
Add a new key to the start of the map.
addToEnd
(key: string, value: any) → OrderedMap
Add a new key to the end of the map.
addBefore
(place: string, key: value: string, value: any) → OrderedMap
Add a key after the given key. If place
is not found, the new
key is added to the end.
forEach
(f: (key: string, value: any))
Call the given function for each key/value pair in the map, in
order.
prepend
(map: Object | OrderedMap) → OrderedMap
Create a new map by prepending the keys in this map that don't
appear in map
before the keys in map
.
append
(map: Object | OrderedMap) → OrderedMap
Create a new map by appending the keys in this map that don't
appear in map
after the keys in map
.
subtract
(map: Object | OrderedMap) → OrderedMap
Create a map containing all the keys in this map that don't
appear in map
.
size
: number
The amount of keys in this map.